class MyPage extends BasePopupEdit {
    constructor(dataId, pageStatus) {
        super(dataId, pageStatus);
    }
    OnPageLoad() {
        let options = [];
        if ((parent._InstancePageLines.mpsItemNode !== null) && (parent._InstancePageLines.mpsItemNode.length > 0)) {
            options.push('<option value="-1">-- 请选择 --</option>');
            for (let i = 0; i < parent._InstancePageLines.mpsItemNode.length; i++) {
                options.push('<option value="',
                        parent._InstancePageLines.mpsItemNode[i].itemId + '">',
                        parent._InstancePageLines.mpsItemNode[i].itemName,
                        '</option>');
            }
            $("#TrackItem").html(options.join(''));
        }
        setTimeout(function () {
            $('#loading').hide();
        }, 300);
    }
    // 得到数据
    GetLinesData() {
        let _result = [];
        // 得到要修改的数据
        parent.$('#tabData11 tbody').find("tr").each(function () {
            if ($(this).hasClass("row__selected")) {
                let item = {};
                item['headerId'] = parent.$("#HeaderId").val();
                item['parentLinesId'] = $(this).attr("_mash_id");
                item['itemId'] = $("#TrackItem").find("option:selected").val();
                item['plan'] = formatNullInputValue($.trim($("#Plan").val()));
                item['actual'] = formatNullInputValue($.trim($("#Actual").val()));
                item['status'] = formatNullInputValue($.trim($("#Status").val()));
                item['updatedBy'] = parent.$("#UserId").val();
                _result.push(item);
            }
        });

        return JSON.stringify(_result);
    }
    // 保存
    DoUpdate() {
        //step 1、进行页面验证，失败则返回
        if (!$("#form_data").valid())
            return;

        //step 2、执行处理
        $.ajax({
            type: "POST",
            url: parent.basePath + "/trade/master_production_schedule/lines/syncupdate.html",
            timeout: 30 * 1000,
            data: this.GetLinesData(),
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            beforeSend: function () {
            }
        }).done(function (data) {
            // 显示明细数据
            parent._InstancePageLines.BuildDataTable(data);
            parent._InstancePageLines.ResetSelectRowStatus();
            ShowSuccessMessagePanel('产品计划跟踪项目设置完成。');
            $(".success-msg").css({left: '180px', top: '8px'});
            //closePopupWnd();
        }).fail(function (data) {
            ajaxError(data, 1);
            $(".error-msg").css({left: '180px', top: '8px'});
        }).always(function ( ) {
        });
    }
}

let  _Instance = null;

//-----------------------------------------------------------------------------
// 页面初始化
//-----------------------------------------------------------------------------
function onPageReady() {
    window.laydate.render({
        elem: '#Plan',
        theme: '#393D49',
        trigger: 'click'
    });
    window.laydate.render({
        elem: '#Actual',
        theme: '#393D49',
        trigger: 'click'
    });

    _Instance = new MyPage(0, 1);
    _Instance.OnPageLoad();
}

// 画面大小或者位置移动
function onPageResize() {
}

//-----------------------------------------------------------------------------
// 验证函数
//-----------------------------------------------------------------------------
$.validator.setDefaults({
    ignore: 'hidden, [reaonly=readonly]'
});
$.validator.addMethod("valueNotEquals", function (value, element, arg) {
    return arg !== value;
}, "Value must not equal arg.");

$(function () {
    $("#form_data").validate({
        // Step 1：设置验证规则和错误消息
        rules: {
            TrackItem: {
                valueNotEquals: "-1",
                required: true
            }
        },
        messages: {
            _TrackItem: {
                valueNotEquals: "请选择跟踪项目。",
                required: "请选择跟踪项目。"
            }
        },
        // Step 2：设置验证触发事件
        onclick: false,
        onfocusout: false,
        onfocusin: false,
        onkeyup: false,
        onkeydown: false,
        focusInvalid: false,
        // Step 3：设置错误信息提示
        showErrors: window.showErrors  //使用自定义的错误显示方法
    });
});